package com.zhou.code;

import com.zhou.code.model.ExcludeField;
import com.zhou.code.model.FileInfo;
import com.zhou.code.model.TempConfig;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TempConfigFactory {

    private static TempConfig tempConfig;

    public static TempConfig getTempConfig() {
        return tempConfig;
    }

    public static void init() throws DocumentException {
        SAXReader reader = new SAXReader();
        File file = new File(Constant.TEMP_FILE_CONFIG);
        Document document = reader.read(file);
        Element root = document.getRootElement();
        List<Element> rootChilds = root.elements();
        tempConfig = new TempConfig();
        List<FileInfo> files = new ArrayList<>();
        List<String> excludeFields = new ArrayList<>();
        List<String> tables = new ArrayList<>();
        Map<String, String> contextValue = new HashMap<>();
        for (Element rootChild : rootChilds) {
            //读取文件配置
            if (rootChild.getName().equals("files")) {
                tempConfig.setFileRoot(rootChild.attribute("root").getStringValue());
                List<Element> fileNodes = rootChild.elements("file");
                for (Element fileNode : fileNodes) {
                    FileInfo fileIno = new FileInfo();
                    fileIno.setAddress(fileNode.attribute("address").getStringValue());
                    fileIno.setFileType(fileNode.attribute("fileType").getStringValue());
                    fileIno.setFileSuffix(fileNode.attribute("fileSuffix").getStringValue());
                    files.add(fileIno);
                }
            //获取不做处理的字段配置
            } else if (rootChild.getName().equals("excludeFields")) {
                List<Element> excludeFiledNodes = rootChild.elements("field");
                for (Element excludeFileNode : excludeFiledNodes) {
                    excludeFields.add(excludeFileNode.attribute("name").getStringValue());
                }
            //获取上下文环境变量的配置
            } else if (rootChild.getName().equals("contextValue")) {
                List<Element> contextNodes = rootChild.elements("value");
                for (Element contextNode : contextNodes) {
                    String name = contextNode.attribute("key").getStringValue();
                    String value = contextNode.attribute("value").getStringValue();
                    contextValue.put(name, value);
                }
            //获取数据源的配置，其中包含了数据库表配置
            } else if (rootChild.getName().equals("dataSource")) {
                List<Element> tableNodes = rootChild.elements("table");
                for (Element tableNode : tableNodes) {
                    tables.add(tableNode.attribute("name").getStringValue());
                }
            } else {
                throw new RuntimeException("不被支持的标签：" + rootChild.getName());
            }
        }
        tempConfig.setContextValue(contextValue);
        tempConfig.setTableNames(tables);
        tempConfig.setFiles(files);
        tempConfig.setExcludeFields(excludeFields);
    }

}
